<script type="text/javascript">
$(function(){
	//未完成
	var pg = App.base.svcDef.ui, pp = pg.params
		, hd = pg.children('.head'), bd = pg.children('.body')
		, gd = pg.seek('grid'), envCb = pg.seek('env');

	gd.mac('grid', {
		cols : [{
			field: 'id', title: '<input type="checkbox" name="all" />', width: 24,
			render : function(r, tr, gd) {
				return '<input type="checkbox" name="keys" value="' + r.id + '"/>';
			}
		},{
			field: 'protocol', title: '协议', width: 120,
			render: function(r){
				return 
			}
		},{
			field: 'template', title: '报文格式', width: 140,
			render: function(r, tr, gd) {
				return $('<span class="a"></span>').click(function(){
					App.open({
						title: '编辑应用实例',
						ctx: 'base',
						action: 'appInstEdit',
						params: $.extend(r, { projId: pp.projId })
					}, function(s){
						gd.load();
					});
				});
			}
		},{
			field: 'template', title: '报文格式', width: 140,
			render: function(r, tr, gd) {
				return $('<span class="a"></span>').click(function(){
					App.open({
						title: '编辑应用实例',
						ctx: 'base',
						action: 'appInstEdit',
						params: $.extend(r, { projId: pp.projId })
					}, function(s){
						gd.load();
					});
				});
			}
		}],
		loader: {
			url: App.ctx + '/app/inst/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				appId: pp.app.id, env: 'ALL'
			},
			autoLoad: false
		},
		pagerLength: 50
	});
	
	pg.seek('all').click(function(){
		gd.seek('keys').prop('checked', $(this).prop('checked'));
	});
	
	if (App.logon.power('app.deploy')) {
		//
		hd.seek('add1').click(function(){
			App.open({
				title: '编辑应用实例',
				ctx: 'base',
				action: 'appInstEdit',
				params: pp
			}, function(s){
				gd.load();
			});
		}).show();
		//
		hd.seek('addN').click(function(){
			App.open({
				title: '批量添加部署节点',
				ctx: 'base',
				action: 'serverPicker',
				model: true,
				params: {
					env: envCb.find('.head input').val(),
					multi: 1,
					projId: pp.app.projId,
					id: pp.app.id
				}
			}, function(s){
				if (!s || !s.length){
					return;
				}
				$.post(App.ctx + '/app/inst/bind.do' ,{
					appIds: pp.app.id,
					serverIds:
						$.map(s, function(n){
							return n.id;
						}).join(',')
				}, function(rs){
					gd.load();
				});
			});
		}).show();
	}
	//
	if (App.logon.power('app.inst.undeploy')) {
		hd.seek('remove').click(function(){
			var keys=gd.seek('keys').filter(':checked');
			if(keys.length>0){
				$.post(App.ctx + '/app/inst/delete.do' ,{
	 				id: keys.map(function(){return $(this).val()}).get().join(',')
	 			}, function(rs){
	 				gd.load();
	 			});
				
			}
		}).show();
	}

	envCb.mac('combo', {
		width: 80,
		boxTop: 34,
		boxWidth: 81,
		readOnly: 1,
		data: Cache.base.env('deploy'),
		onSelect: function(me, sk, sd){
			gd.load({ env: envCb.data[sk[0]] });
		}
	}).val(0);
	
	pg.seek('key').keydown(function(e) {
		if (e.keyCode == 13){
			gd.load({ key : $(this).val() });
		}
	});
	pg.seek('find').click(function(e){
		gd.load({ key : pg.seek('key').val() });
	});
	
	if (!App.websocket.inst) {
		App.websocket.open();
	}
	App.websocket.on('deploy', 'log', 'appInstIndex', function(o, e, ws) {
		var ib = App.inbox, p = ib.seek(o.token);
		ib.children('pre').hide();
		if (!p.length) {
			p = $('<pre></pre>').attr('name', o.token).appendTo(ib);
		}
		p.append(o.data).show();
	});
	App.websocket.on('deploy', 'event', 'appInstIndex', function(o, e, ws) {
		gd.load();
	});
	App.websocket.on('app', 'inst', 'appInstIndex', function(o, e, ws) {
		gd.load();
	});
	
	function adjust(){
		var h = pg.parent().height();
		bd.height(h - hd.height() - 64);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="head">
	<div class="fl">
		<button class="hidden" name="add1">添加部署节点</button>
		<button class="hidden" name="addN">批量添加部署节点</button>
		<button class="hidden" name="remove">批量移除</button>
	</div>
	<div class="fl">
		<span class="fl">环境切换：</span>
		<div class="combo fl" name="env" style="margin-top: 3px;"></div>
	</div>
	<div class="fl">
		&nbsp;&nbsp;&nbsp;&nbsp;关键字： <input type="text" name="key" />
		<button name="find">查找</button>
	</div>
	<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid" name="grid"></div>
</div>